package com.weactivity.service.impl;

import com.weactivity.dto.AdduserDTO;
import com.weactivity.dto.DeleteDTO;
import com.weactivity.dto.ManageUserDTO;
import com.weactivity.entity.Identity;
import com.weactivity.mapper.ManageUserMapper;
import com.weactivity.service.ManageUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ManageUserServiceImpl implements ManageUserService {
    @Autowired
    ManageUserMapper manageUserMapper;

    /**
     * 更改身份
     *
     * @param manageUserDTO
     * @return
     */
    @Override
    public void changeIdentityByAid(ManageUserDTO manageUserDTO) {
        Identity identity1 = new Identity();
        int uid = manageUserDTO.getUid();
        int toRole = manageUserDTO.getToRole();
        identity1.setAid(manageUserDTO.getAid());
        identity1.setUid(uid);
        int nowRole = manageUserMapper.getNowRole(identity1);
        int aid = manageUserDTO.getAid();
        Identity identity = new Identity();
        identity.setUid(uid);
        identity.setAid(aid);
        identity.setRole(toRole);
        identity.setPower(toRole);
        identity.setTemp(0);
        if(0 == nowRole) {
            manageUserMapper.deleteOrganizer(identity);
        }
        else if(1 == nowRole){
            manageUserMapper.deleteManger(identity);
        }
        else if(2 == nowRole){
            manageUserMapper.deleteParticipant(identity);
        }

        if(0 == toRole) {
            manageUserMapper.insertOrganizer(identity);
        }
        else if(1 == toRole){
            manageUserMapper.insertManger(identity);
        }
        else if(2 == toRole){
            manageUserMapper.insertParticipant(identity);
        }

        manageUserMapper.updateRole(identity);

    }

    /**
     * 添加用户
     *
     * @param adduserDTO
     * @return
     */
    @Override
    public void add(AdduserDTO adduserDTO) {
        Identity identity = new Identity();
        BeanUtils.copyProperties(adduserDTO,identity);
        identity.setTemp(0);
        manageUserMapper.insertParticipant(identity);
        manageUserMapper.addRole(identity);
    }

    /**
     * 删除用户
     *
     * @param manageUserDTO
     * @return
     */
    @Override
    public void deleteByUid(DeleteDTO manageUserDTO) {
        Identity identity=new Identity();
        identity.setAid(manageUserDTO.getAid());
        identity.setUid(manageUserDTO.getUid());
        int role = manageUserMapper.getNowRole(identity);
        if(0 == role) {
            manageUserMapper.deleteOrganizer(identity);
        }
        else if(1 == role){
            manageUserMapper.deleteManger(identity);
        }
        else if(2 == role){
            manageUserMapper.deleteParticipant(identity);
        }

        manageUserMapper.deleteRole(identity);
    }
}
